Libraries

The following libraries were used:

library(tidyverse)
library(here)
library(plotly)
library(htmlwidgets)

Adding size data

load(file = here("data/data_tidy.RData")) # load object data_tidy

BodyLengths <- tibble(Species = c("Ceriodaphnia sp.", "C. megalops", "D. cucullata", "D. curvirostris", "D. hyalina var. gellata", "D. hyalina var. lacustris", "D. hyalina", "D. longispina", "D. pulex", "D. magna", "Calanoid copepods", "Cyclops", "Bosmina coregoni", "B. longirostris", "Sida sp.", "S. crystallina", "Chydorus ovalis", "Eurycercus lamellatus", "Alona spp.", "A. quadrangularis", "Asplanchna", "Keratella spp.", "Ostracod", "Diaptomus"),
                      BodyLength = c(0.4636, 0.925, 0.8593, 1.58, 0.8982, 0.8982, 0.8982, 1.0347, 1.118, 1.4332, 1.0024, 0.9334, 0.423, 0.3838, 0.5075, 0.5075, 0.475, 1.975, 0.7018, 0.8802, 0.4747, 0.3495, 1.25, 1.0024)) # species and their body sizes

data <- left_join(data_tidy, BodyLengths, by = "Species") # add species body size to the dataframe

data <- mutate(data, SizeClass = case_when(BodyLength <= 0.6 ~ "Small (<= 0.6 mm)",
                                           BodyLength <= 1.0 ~ "Medium ( 0.6 < x <= 1.0 mm)",
                                           BodyLength > 1.0 ~ "Large (> 1.0 mm)")) # define different categories of body sizes

Data wrangling and creating plots

Using nested for loops, R cycles through all six lakes and year by year. It then creates plots of all possible combinations.

lakes <- unique(data_tidy$LakeName) # extract the names of the lakes
years <- unique(data_tidy$Year) # extract the years where counts were measured

plotlist <- list() # create an empty list to add to

for (i in 1:length(lakes)){ # perform the analysis per lake
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){ # perform the analysis per year
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) { # check if there is any data for this lake/year combination
      
      Xdata <- Ydata %>% mutate("Abundance_ind/L" = (Counts / Proportion_of_sample_counted) / Sampling_volume_net_L,
                               "Relative_abundance" = Counts / Total_individuals * 100) # calculate the abundances and relative abundances
      
      summary <- Xdata %>% group_by(Species, SizeClass, Month) %>% summarize(sum_counts = sum(Counts, na.rm = TRUE),
                                                                 sum_total = sum(Total_individuals, na.rm = TRUE)) # calculate counts per month per species
      
      summary <- summary %>% group_by(SizeClass, Month, sum_total) %>% summarize(sum_counts = sum(sum_counts, na.rm = TRUE)) # calculate counts per month per size class
      
      summary <- summary %>% mutate("Relative_abundance" = sum_counts / sum_total * 100) # Calculate relative abundance per month
      
      result <- summary %>% ggplot(aes(x = Month, y = Relative_abundance, fill = SizeClass))+
        geom_col(color = "Black", linewidth = 0.05)+
        labs(y = "Relative abundance (%)", title = paste0(lakes[[i]], " in ", years[[j]]))+
        theme_minimal() # generate plot of relative abundance per size class per month
      
      plotlist[[(length(plotlist)+1)]] <- result # add the plot to the list of plots
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]])) # when there is no data for a lake/year combination, a warning is printed
    }
  }
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012

Interactive plots

The {plotly} package was used to create interactive plots, to make it possible to see the actual values of a bar.

htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))